<?php

namespace app\forum\controller;

use  app\admin\controller\Admin as BaseAdmin;
use app\admin\builder\Form;
use app\admin\builder\Table;

class Admin extends BaseAdmin
{
    public function initialize()
    {
        parent::initialize();
    }

    public function _leftSlide()
    {

        $slide = [
            ['title' => '论坛配置', 'child' => [
                ['menu' => 'forum/admin/config', 'query' => ['id' => 'forum'], 'title' => '论坛配置', 'slide_name' => 'config'],
            ]],
            /*            ['title' => '表格', 'child' => [
                            ['menu' => 'admin/index/table', 'title' => '表格', 'slide_name' => 'table'],
                        ]]*/
        ];

        return $slide;
    }


    public function config()
    {
        $form = new Form();
        $moduleName = input('id', '', 'text');
        if (Request()->isPost()) {
            $input = input();
            $time = time();
            foreach ($input as &$v) {
                $v = text($v); //防止注入
            }
            unset($v);
            $input['update_time'] = $time;
            $module = db('module')->where('name', $moduleName)->find();
            if ($module && $module['status'] == 1) {
                db('module')->where('name', $moduleName)->update(['config' => json_encode($input, JSON_UNESCAPED_UNICODE)]);
                $this->success('修改成功');
            }
            $this->error('修改失败，请确保模块已经安装');

        } else {

            $this->setBreadcrumb([
                ['href' => url('module/modules'), 'title' => '模块列表'],
            ]);

            $info = require_once($this->appPath . '/' . parse_name($moduleName, 1) . '/config.php');
            $list = $info['config'];
            $config = db('module')->where('name', $moduleName)->value('config');
            $config = json_decode($config, true);
            $data = [
                'id' => $moduleName
            ];
            //$data =[];
            $this->_setSlide('config');
            $form->setTitle($info['title'])
                ->form(url('config'))->typeText('id', '模块标识', ['attr' => 'readonly']);
            foreach ($list as $v) {
                $data[$v['name']] = isset($config[$v['name']]) ? $config[$v['name']] : $v['default'];
                $func = 'type' . $v['type'];
                if (method_exists($form, $func)) {
                    $form->$func($v['name'], $v['title'], $v['extra']);
                }
            }
            return $form->setData($data)->show();
        }
    }

    public function index()
    {
        return view('index');
    }

}
